var util = require('util');
var log4js = require('log4js');
var mkdirp = require('mkdirp');

function Logger(){
    mkdirp.sync('logs');
    log4js.configure({
        "appenders": [
            {
                "type": "console"
            },
            {
                "type": "file",
                "filename": "logs/logs.log",
                "maxLogSize": 2097152,
                "category": "logger"
            },
            {
                "type": "file",
                "filename": "logs/access.log",
                "maxLogSize": 2097152,
                "category": "accessLog"
            },
            {
                "type": "file",
                "filename": "logs/error.log",
                "maxLogSize": 2097152,
                "category": "errorLog"
            }
        ]
    });

    this.errorLog = log4js.getLogger('errorLog');
    this.logger = log4js.getLogger('logger');
    this.accessLog = log4js.getLogger('accessLog');
}

Logger.prototype.shutdown = function(){
    log4js.shutdown(function () {
        console.log('logger shutdown.');
    });
};

Logger.prototype.access = function(){
    this.accessLog.info(util.format.apply(util,arguments));
};

Logger.prototype.info = function(){
    this.logger.info(util.format.apply(util,arguments));
};

Logger.prototype.debug = function(){
    this.logger.debug(util.format.apply(util,arguments));
};

Logger.prototype.warn = function(){
    this.logger.warn(util.format.apply(util,arguments));
};

Logger.prototype.error = function(){
    this.errorLog.error(util.format.apply(util,arguments));
};

Logger.prototype.log = function(){
    this.logger.info(util.format.apply(util,arguments));
};

Logger.prototype.trace = function(){
    this.logger.info(util.format.apply(util,arguments));
};

Logger.prototype.fatal = function(){
    this.logger.error(util.format.apply(util,arguments));
};

Logger.prototype.stack = function(){
    this.logger.error((new Error()).stack);
};

module.exports = exports = new Logger();
